package com.woniuxy.oa.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.oa.entity.Attendance;
import com.woniuxy.oa.entity.Employee;
import com.woniuxy.oa.entity.Overtime;
import com.woniuxy.oa.entity.PageBean;
import com.woniuxy.oa.service.OvertimeService;
import com.woniuxy.oa.utils.CurrentDateUtils;
import com.woniuxy.oa.utils.GetOvertimeUtils;

@Controller
//配置命名空间
@RequestMapping("/Overtime")
public class OvertimeController {
	
	@Autowired
	OvertimeService overtimeService;

	public void setOvertimeService(OvertimeService overtimeService) {
		this.overtimeService = overtimeService;
	}
	
	//添加加班信息
	@ResponseBody
	@RequestMapping("/addOvertimeInfo")
	public Integer addOvertime(HttpServletRequest request) {
		System.out.println("正在执行添加方法");
		Overtime overtime = new Overtime();
		Integer result = 0;
		try {
			//获取页面参数
			//获取用户id和用户名（从域中取出）
//			Employee emp = (Employee) request.getSession().getAttribute("user");
//			Integer userid = emp.getId();
//			String username = emp.getUsername();
			
			//获取当前的日期为申请时间
			String applicationTime = CurrentDateUtils.getCurrentDateAndTime(); 
			//开始时间，结束时间从页面获取，
			String beginTime = request.getParameter("beginTime");
			String endTime = request.getParameter("endTime");
			//根据开始时间和结束时间计算加班时长
			String overtimehours = GetOvertimeUtils.countTime(beginTime, endTime);
			//加班描述
			String des = request.getParameter("des");
			//审批人
			
			//审批状态，从审批表里面查询
			
			
			overtime.setOm_userid(1);
			overtime.setOm_username("王保长");
			overtime.setOm_applicationtime(applicationTime);
			overtime.setOm_starttime(beginTime);
			overtime.setOm_endtime(endTime);
			overtime.setOm_overtimehours(overtimehours);
			overtime.setOm_description(des);
			overtime.setOm_approvalstatus(1);
			//非空判断
			if(beginTime==null || endTime==null || des==null) {
				//添加失败，所选项不能为空
				return 2;
			}else {
				result = overtimeService.addOvertimeInfo(overtime);
				return result; 
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", e.getMessage());
		}
		return result;
	}
	
	//修改加班信息
	@ResponseBody
	@RequestMapping("/modifyOvertime")
	public Integer updateOvertimeInfo(HttpServletRequest request) {
		
		Integer result = 0;//默认修改不成功
		Overtime overtime = new Overtime();
		try {
			//获取页面参数
			//开始时间，结束时间从页面获取，
			String beginTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			//根据开始时间和结束时间计算加班时长
			String overtimehours = GetOvertimeUtils.countTime(beginTime, endTime);
			//加班描述
			String des = request.getParameter("des");
			//获取当前修改列的id
			String id = request.getParameter("id");
			Integer om_id = Integer.parseInt(id);
			overtime.setOm_starttime(beginTime);
			overtime.setOm_endtime(endTime);
			overtime.setOm_overtimehours(overtimehours);
			overtime.setOm_description(des);
			overtime.setOm_id(om_id);
			
			result = overtimeService.modifyOvertimeInfo(overtime );
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", e.getMessage());
		}
		return result;
	}
	
	//删除加班信息
	@ResponseBody
	@RequestMapping("/deleteOvertime")
	public Integer deleteOvertimeInfo(HttpServletRequest request) {
		
		Integer result = 0;
		try {
			//获取到当前删除行的id
			String id = request.getParameter("id");
			Integer om_id = Integer.parseInt(id);
			result = overtimeService.removeOvertimeInfoByOm_id(om_id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据姓名分页展示数据
	 */
	
	//从页面上获取url路径参数
	public String getUrl(HttpServletRequest request) {
		//获取url中的工程名
		String contextPath = request.getContextPath();
		//获取url中的controller映射名
		String servletPath = request.getServletPath();
		//获取url中的参数
		String queryString = request.getQueryString();
		//获取到的url参数中不包括pageIndex数据，因为这个数据使我们通过点击页面上的链接得到的，因此需要去掉这个数据
		if(queryString!=null && queryString.indexOf("&pageIndex=")!=-1) {
			//包含pageIndex数据，就截取该参数
			queryString = queryString.substring(0, queryString.indexOf("&pageIndex="));
		}
		System.out.println("getUrl----"+(contextPath+servletPath+"?"+queryString));
		return contextPath+servletPath+"?"+queryString;
	}
	
	//获取用户传参,封装为一个方法
	private int getPageIndex(HttpServletRequest request) {
		if(request.getParameter("pageIndex")==null || request.getParameter("pageIndex").equals("")) {
			//说明用户没有传参，我们默认传入1
			return 1;
		}
		//用户传参
		return Integer.parseInt(request.getParameter("pageIndex"));
	}
	
	//根据姓名查询加班信息，并做分页展示
	@ResponseBody
	@RequestMapping("/showOvertimeByPage")
	public PageBean<Overtime> getOvertimeByPage(HttpServletRequest request){
		
		Overtime overtime = new Overtime();
		PageBean<Overtime> pageBean = null;
		try {
			
			//获取用户传入的当前页
			int pageIndex = getPageIndex(request);
			int pageSize = 10;
			//从页面获取查询的用户姓名
			String username = request.getParameter("username");
			overtime.setOm_username(username);
			
			pageBean = overtimeService.getOvertimeInfoByNameAndPage(pageIndex, pageSize, overtime);
			pageBean.setUrl(getUrl(request));
			return pageBean;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", e.getMessage());
			return pageBean;
		}
	}
	
	
}
